package com.ghxxzx.dagl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ghxxzx.dagl.entity.Menue;
import com.ghxxzx.dagl.entity.Role;
import com.ghxxzx.dagl.entity.RoleMenue;
import com.ghxxzx.dagl.entity.User;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.MenueMapper;
import com.ghxxzx.dagl.mapper.RoleMapper;
import com.ghxxzx.dagl.mapper.RoleMenueMapper;
import com.ghxxzx.dagl.mapper.UserMapper;
import com.ghxxzx.dagl.vo.*;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * desc:
 *
 * @author cx316
 * Created by cx316 on 2020-05-27.
 */
@Service
@Transactional
public class UserServiceImpl {

    @Autowired
    UserMapper userMapper;
    @Autowired
    MenueMapper menueMapper;
    @Autowired
    RoleMenueMapper roleMenueMapper;
    @Autowired
    RoleMapper roleMapper;

    public void addUser(User user){
        user.setId(null);
        String name = user.getName();
        String phone = user.getPhone();
        User byNameAndPhone = userMapper.findByNameAndPhone(name, phone);
        if(byNameAndPhone!=null){
            throw new JyException("该用户已存在系统中");
        }
        User accountUser = userMapper.findByAccount(user.getAccount());
        if(accountUser!=null){
            throw new JyException("账户已存在");
        }
        userMapper.insert(user);
    }

    public Page selectPage(PageQuery pageQuery,String keyword) {
        Page page = new Page(pageQuery.getPageNum(),pageQuery.getPageSize());
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.ne("role_id",1);
        if (keyword!=null){
            userQueryWrapper.like("name",keyword);
        }
        userMapper.selectPage(page,userQueryWrapper);
        return page;
    }

    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }

    public List<User> selectAllShry() {
        return userMapper.selectShry();
    }

    public ResultEntity addRole(String name, List<Integer> ids) {
        Role role = roleMapper.findByName(name);
        if (role!=null){
            throw new JyException("该重复名称已存在");
        }
        Role role1 = new Role(null, name);
        int insert = roleMapper.insert(role1);
        for (int i = 0; i < ids.size(); i++) {
            roleMenueMapper.insert(new RoleMenue(null,role1.getId(),ids.get(i)));
        }
        return ResultEntity.ok();
    }

    public ResultEntity updataQueryUser(Integer id) {
        User user = userMapper.selectById(id);
        Role role = roleMapper.selectById(user.getRoleId());

        return ResultEntity.ok(new UserVo(user,role));
    }

    public ResultEntity updataUser(User user) {
        userMapper.updateById(user);
        return ResultEntity.ok();
    }

    public ResultEntity updateRoleQuery(Integer id) {
        Role role = roleMapper.selectById(id);
        List<RoleMenue> menueList = roleMenueMapper.findRoleIdAll(role.getId());
        List<Menue> all = menueMapper.findAll();
        LxNode lxNode = new LxNode(role.getId() + "", role.getName());
        ArrayList<LxNode> memue = getMemue(all, menueList);
        lxNode.setNodes(memue);
        return ResultEntity.ok(lxNode);
    }
    public ArrayList<LxNode> getMemue(List<Menue> all,List<RoleMenue> roleMenues){
        ArrayList<LxNode> lxNodes = new ArrayList<>();
        ArrayList<Menue> menues = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            Menue menue = all.get(i);
            if (menue.getParentId()==0){
                menues.add(menue);
            }
        }
        for (int i = 0; i < menues.size(); i++) {
            all.remove(menues.get(i));
        }
        for (int i = 0; i < menues.size(); i++) {
            Menue menue = menues.get(i);
            LxNode lxNode = new LxNode(menue.getId() + "", menue.getName());
            lxNodes.add(lxNode);
            for (int i1 = 0; i1 < roleMenues.size(); i1++) {
                if (menue.getId()==roleMenues.get(i1).getMenueId()){
                    lxNode.initCheck();
                }
            }
        }
        for (int i = 0; i < lxNodes.size(); i++) {
            LxNode lxNode = lxNodes.get(i);
            ArrayList<LxNode> lxNodes1 = new ArrayList<>();
            for (int i1 = 0; i1 < all.size(); i1++) {
                Menue menue1 = all.get(i1);
                if (Integer.parseInt(lxNode.getId())==(menue1.getParentId())){
                    LxNode lxNode1 = new LxNode(menue1.getId() + "", menue1.getName());
                    lxNodes1.add(lxNode1);
                    for (int i3 = 0; i3 < roleMenues.size(); i3++) {
                        if (menue1.getId()==roleMenues.get(i3).getMenueId()){
                            lxNode1.initCheck();
                        }
                    }
                }
            }
            lxNode.setNodes(lxNodes1);
        }
        return lxNodes;
    }

    public ResultEntity queryMenueAll() {
        List<Menue> all = menueMapper.findAll();
        ArrayList<LxNode> memue = getMemue(all);
        return ResultEntity.ok(memue);
    }
    public ArrayList<LxNode> getMemue(List<Menue> all){
        ArrayList<LxNode> lxNodes = new ArrayList<>();
        ArrayList<Menue> menues = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            Menue menue = all.get(i);
            if (menue.getParentId()==0){
                menues.add(menue);
            }
        }
        for (int i = 0; i < menues.size(); i++) {
            all.remove(menues.get(i));
        }
        for (int i = 0; i < menues.size(); i++) {
            Menue menue = menues.get(i);
            lxNodes.add(new LxNode(menue.getId()+"",menue.getName()));
        }
        for (int i = 0; i < lxNodes.size(); i++) {
            LxNode lxNode = lxNodes.get(i);
            ArrayList<LxNode> lxNodes1 = new ArrayList<>();
            for (int i1 = 0; i1 < all.size(); i1++) {
                Menue menue1 = all.get(i1);
                if (Integer.parseInt(lxNode.getId())==(menue1.getParentId())){
                    lxNodes1.add(new LxNode(menue1.getId()+"",menue1.getName()));
                }
            }
            lxNode.setNodes(lxNodes1);
        }
        return lxNodes;
    }

    public ResultEntity queryRoleAll() {
        List<Role> all = roleMapper.findAll();
        ArrayList<RoleListVo> list = new ArrayList<>();
        for (int i = 0; i < all.size(); i++) {
            Role role = all.get(i);
            List<String> menues = menueMapper.findRoleNameMenueId(role.getId());
            list.add(new RoleListVo(role.getId(),role.getName(), Joiner.on("、").join(menues)));
        }
        return ResultEntity.ok(list);
    }

    public ResultEntity updateRole(Role role, ArrayList<Integer> ids) {
        Role selectById = roleMapper.selectById(role.getId());
        selectById.setName(role.getName());
        roleMapper.updateById(selectById);
        roleMenueMapper.deleteByRoleId(selectById.getId());
        for (int i = 0; i < ids.size(); i++) {
            roleMenueMapper.insert(new RoleMenue(null,selectById.getId(),ids.get(i)));
        }
        return ResultEntity.ok();
    }

    public void deleteRole(Integer roleId) {
        roleMapper.deleteById(roleId);
    }

    public ResultEntity querRole() {
        List<Role> all = roleMapper.findNotRoot();
        return ResultEntity.ok(all);
    }

    public List<User> listRgShUser() {
        List<User> users = userMapper.selectRgShry();
        return users;
    }
}
